using Microsoft.SqlServer.Management.Smo;
using System;
using System.Globalization;
using System.Reflection;

namespace Microsoft.SqlServer.Management.SqlManagerUI.Scripting
{
	internal class ReflectionUtilities
	{
		private const string version = "10.0.0.0";

		public const string SQLMgmtAssemblyName = "sqlmgmt, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL";

		private static AssemblyName _SQLEnumAssemblyName = new AssemblyName("Microsoft.SqlServer.SqlEnum, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL");

		internal static string EscapeString(string itemContext, char ch)
		{
			string text = ch.ToString();
			return itemContext.Replace(text, text + text);
		}

		internal static object InvokeMethod(object obj, string methodName, object[] parameters)
		{
			object result;
			try
			{
				Type type = obj.GetType();
				MethodInfo method = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
				result = method.Invoke(obj, parameters);
			}
			catch (TargetInvocationException ex)
			{
				if (ex.InnerException is UnsupportedVersionException)
				{
					string message = string.Empty;
					if (obj is Table)
					{
						message = string.Format(CultureInfo.CurrentCulture, ScriptingEngineResources.ERROR_TableNotSupportedInTargetVersion, new object[]
						{
							((Table)obj).Name
						});
					}
					else
					{
						message = string.Format(CultureInfo.CurrentCulture, ScriptingEngineResources.ERROR_ObjectNotSupportedInTargetVersion, new object[]
						{
							obj
						});
					}
					throw new UnsupportedVersionException(message, ex);
				}
				if (ex.InnerException != null)
				{
					throw ex.InnerException;
				}
				throw;
			}
			return result;
		}
	}
}
